package com.system.outpatientsystem.controller;

import com.system.outpatientsystem.pojo.Result;
import com.system.outpatientsystem.pojo.User;
import com.system.outpatientsystem.service.UserService;
import com.system.outpatientsystem.utils.JwtUtils;
import com.system.outpatientsystem.utils.MD5Utils;
import com.system.outpatientsystem.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/findById")
    public User findById(Integer id){
        return userService.findById(id);
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        //查询用户
        User u = userService.findByPhone(user.getPhone());
        System.out.println(user.getPhone());
        if (u == null){
            System.out.println("用户不存在");
            return Result.error("用户不存在");
        }
        //判断密码
//        String md5String = MD5Utils.MD5Upper(user.getPassword());
//        System.out.println(md5String);
        if (u.getPassword().equals(user.getPassword())){
            //登录成功
            System.out.println("登录成功");
//            Map<String,Object> claims = new HashMap<>();
//            claims.put("id",u.getId());
//            claims.put("phone",u.getPhone());
//            String token = JwtUtils.genToken(claims);
            return Result.success(u);
        }
        //密码错误
        System.out.println("密码错误");
        return Result.error("密码错误");
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user){
        //查询用户
        System.out.println(user.getPhone());
        System.out.println(user.getId());
        User u = userService.findByPhone(user.getPhone());
        if (u == null){
            //注册
            userService.register(user);
            return Result.success();
        }else{
            return Result.error("用户已存在");
        }
    }

    @GetMapping("/userInfo")
    public Result<User> userInfo(){
        //根据用户phone查询用户
        Map<String, Object> map = ThreadLocalUtil.get();
        String phone = (String) map.get("phone");
        User user = userService.findByPhone(phone);
        return Result.success(user);
    }

    @PutMapping("/userUpdate")
    public Result userUpdate(@RequestBody @Validated User user){   //@RequestBody把请求体的数据转为实体类对象
        //更新用户信息
        userService.userUpdate(user);
        return Result.success();
    }

    @PatchMapping("/userUpdatePwd")
    public Result userUpdatePwd(@RequestBody Map<String,String> params){
        //更新用户密码
        //1.校验参数
        String old_pwd = params.get("old_pwd");
        String new_pwd = params.get("new_pwd");
        String re_pwd = params.get("re_pwd");

        if (!StringUtils.hasLength(old_pwd) || !StringUtils.hasLength(new_pwd)|| !StringUtils.hasLength(re_pwd)){
            return Result.error("缺少必要的参数");
        }

        Map<String, Object> map = ThreadLocalUtil.get();
        String phone = (String) map.get("phone");
        User byPhone = userService.findByPhone(phone);
        if (!byPhone.getPassword().equals(MD5Utils.MD5Upper(old_pwd))){
            return Result.error("原密码错误");
        }

        if (!new_pwd.equals(re_pwd)){
            return Result.error("两次输入的新密码不一致");
        }

        //2.调用service完成密码更新
        userService.userUpdatePwd(new_pwd);
        return Result.success();
    }
}







